package com.baohulu.baohulu.activity.userprofile;

import android.os.Bundle;
import android.text.TextUtils;
import android.widget.EditText;

import com.baohulu.baohulu.R;
import com.baohulu.baohulu.base.BaseActivity;
import com.baohulu.baohulu.base.BaseResponseCallback;
import com.baohulu.baohulu.utils.HttpUtil;
import com.baohulu.baohulu.utils.JsonUtil;
import com.baohulu.baohulu.utils.ToastUtil;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class UpdatePasswordActivity extends BaseActivity {
    @BindView(R.id.et_old_password)
    EditText mEtOldPassword;
    @BindView(R.id.et_new_password)
    EditText mEtNewPassword;
    @BindView(R.id.et_confirm_password)
    EditText mEtConfirmPassword;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update_password);
        ButterKnife.bind(this);
    }

    @OnClick(R.id.confirm)
    public void updatePassword() {
        String oldPassword = mEtOldPassword.getText().toString();
        String newPassword = mEtNewPassword.getText().toString();
        String confirmPassword = mEtConfirmPassword.getText().toString();
        if (isInputValid(oldPassword, newPassword, confirmPassword)) {
            submitData(oldPassword, newPassword);
        }
    }

    private void submitData(String oldPassword, String newPassword) {
        String url = getString(R.string.url_base) + getString(R.string.url_update_password);
        JSONObject data = new JsonUtil.JsonBuilder()
                .put("password", oldPassword)
                .put("new_password", newPassword)
                .build();
        HttpUtil.post(this, url, data, new BaseResponseCallback() {
            @Override
            public void onStart() {
                showProgressDialog();
            }

            @Override
            public void onJsonObjectResponse(JSONObject jsonObject) {
                super.onJsonObjectResponse(jsonObject);
                try {
                    int state = jsonObject.getInt("state");
                    resolveUpdateResult(state);
                } catch (JSONException e) {
                    e.printStackTrace();
                } finally {
                    hideProgressDialog();
                }
            }

            @Override
            public void onFailure(String msg) {
                hideProgressDialog();
                ToastUtil.showToast(msg);
            }
        });
    }

    private void resolveUpdateResult(int state) {
        if (state == 0) {
            ToastUtil.showToast("原密码错误！");
        } else {
            ToastUtil.showToast("修改成功！");
            finish();
        }

    }

    private boolean isInputValid(String oldPassword, String newPassword, String confirmPassword) {
        if (TextUtils.isEmpty(oldPassword) || TextUtils.isEmpty(newPassword) || TextUtils.isEmpty(confirmPassword)) {
            ToastUtil.showToast("输入不能为空！");
        } else if (newPassword.length() < 6) {
            ToastUtil.showToast("密码长度不能小于6位！");
        } else if (!newPassword.equals(confirmPassword)) {
            ToastUtil.showToast("两次输入密码不一致！");
        } else {
            return true;
        }
        return false;
    }
}
